﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

/// <summary>数据 列表 解析</summary>
public class CustomDataListLoader
{
	public string Mlist_path;
	/// <summary> 分隔符 </summary>
	private char[] separator = new char[] { '\t' };
	/// <summary> 属性 列 </summary>
	private List<string> attributesList = new List<string>();
	/// <summary> 属性 字典 </summary>
	private Dictionary<string, int> attributeDictionary = new Dictionary<string, int>();
	/// <summary> 数据</summary>
	private List<string[]> datas = new List<string[]>();
	public void SetSeparator(char separator)
	{
		this.separator = new char[] { separator };
	}

	public void SetSeparator(char[] separator)
	{
		this.separator = separator;
	}

	/// <summary> 加载文本</summary>
	public bool Load(TextAsset text)
	{
		StringReader stringReader = new StringReader(text.text);
		bool result = this.Load(stringReader);
		stringReader.Close();
		return result;
	}

	public bool Load(string fullPath)
	{
		FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
		if (fileStream == null)
		{
			MonoBehaviour.print("读取失败: " + fullPath);
			return false;
		}
		StreamReader streamReader = new StreamReader(fileStream);
		bool result = this.Load(streamReader);
		streamReader.Close();
		return result;
	}

	private bool Load(TextReader reader)
	{
		string text = reader.ReadLine();
		if (text == null) return false;

		string[] array = text.Split(this.separator, StringSplitOptions.RemoveEmptyEntries);
		for (int i = 0; i < array.Length; i++)
		{
			this.attributesList.Add(array[i]);
			this.attributeDictionary.Add(array[i], i);
		}
		int count = this.attributesList.Count;
		while ((text = reader.ReadLine()) != null)
		{
			if (text.Length != 0)
			{
				array = text.Split(this.separator, StringSplitOptions.RemoveEmptyEntries);
				if (array.Length != count)
				{
					GlobalData.Debug_LogWarning("列表不完整:" + text);
				}
				else
				{
					string[] array2 = new string[count];
					for (int j = 0; j < count; j++)
						array2[j] = array[j];
					this.datas.Add(array2);
				}
			}
		}
		reader.Close();
		return true;
	}

	public int GetAttributeNo(string attribute)
	{
		if (this.attributeDictionary.ContainsKey(attribute))
		{
			return this.attributeDictionary[attribute];
		}
		return -1;
	}

	public string GetData(string attribute, int y)
	{
		int attributeNo = this.GetAttributeNo(attribute);
		if (attributeNo == -1)
		{
			GlobalData.Debug_LogError("不明");
			return string.Empty;
		}
		return this.GetData(attributeNo, y);
	}

	public string GetData(int x, int y)
	{
		return this.datas[y][x];
	}

	public int GetDataNum()
	{
		return this.datas.Count;
	}


}
